package com.zrwl.poscloud.xo.service.impl;
import com.zrwl.poscloud.base.entity.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.EmployeeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import com.zrwl.poscloud.xo.mapper.StudentScoreTotalDetailMapper;
import com.zrwl.poscloud.xo.vo.StudentScoreTotalDetailVO;

import javax.annotation.Resource;

/**
 * 总分明细 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-10
 */
@Service
public class StudentScoreTotalDetailServiceImpl extends SuperServiceImpl<StudentScoreTotalDetailMapper,StudentScoreTotalDetail> implements StudentScoreTotalDetailService {

    @Autowired
    private StudentScoreTotalDetailService studentScoreTotalDetailService;
    @Resource
    private StudentScoreTotalDetailMapper studentScoreTotalDetailMapper;
    @Autowired
    private GradeTeacherRService gradeTeacherRService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private CompanyService companyService;
    @Override
    public IPage<StudentScoreTotalDetailVO> getPageList(StudentScoreTotalDetailVO studentScoreTotalDetailVO){
       Page<StudentScoreTotalDetail> page = new Page<>();
       page.setCurrent(studentScoreTotalDetailVO.getCurrentPage());
       page.setSize(studentScoreTotalDetailVO.getPageSize());
       IPage<StudentScoreTotalDetailVO> pageList = studentScoreTotalDetailMapper.getPageList(page, studentScoreTotalDetailVO);
       List<StudentScoreTotalDetailVO> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<StudentScoreTotalDetail> getAllList(StudentScoreTotalDetailVO studentScoreTotalDetailVO){
        QueryWrapper<StudentScoreTotalDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<StudentScoreTotalDetail> list = studentScoreTotalDetailService.list(queryWrapper);
        return list;
}
    @Override
    public String add(StudentScoreTotalDetailVO studentScoreTotalDetailVO){

        StudentScoreTotalDetail entity = new StudentScoreTotalDetail();
        BeanUtils.copyProperties(studentScoreTotalDetailVO, entity, SysConf.STATUS);
        entity.insert();
        studentScoreTotalDetailVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);}
    @Override
    public String edit(StudentScoreTotalDetailVO studentScoreTotalDetailVO){
        StudentScoreTotalDetail entity = studentScoreTotalDetailService.getById(studentScoreTotalDetailVO.getUid());
        BeanUtils.copyProperties(studentScoreTotalDetailVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(StudentScoreTotalDetailVO studentScoreTotalDetailVO){
        StudentScoreTotalDetail entity = studentScoreTotalDetailService.getById(studentScoreTotalDetailVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}

    @Override
    public String addScore(StudentScoreTotalDetailVO studentScoreTotalDetailVO) {
        GradeTeacherR teacher = gradeTeacherRService.getTeacherBySubjectTypeAndGrade(studentScoreTotalDetailVO.getDeptUid(), studentScoreTotalDetailVO.getSubjectType());
        studentScoreTotalDetailVO.setTeacherUid(teacher.getUid());
        StudentScoreTotalDetail entity = studentScoreTotalDetailMapper.getTotalDetail(studentScoreTotalDetailVO);
        if(entity != null){
            entity.setTeacherUid(studentScoreTotalDetailVO.getTeacherUid());
            entity.setTotalScore(entity.getTotalScore()+studentScoreTotalDetailVO.getTotalScore());
            entity.setTotalFull(entity.getTotalFull()+studentScoreTotalDetailVO.getTotalFull());
            entity.updateById();
        }else {
            add(studentScoreTotalDetailVO);
        }
        return ResultUtil.successWithMessage("更新成功");
    }

    @Override
    public String editScore(StudentScoreTotalDetailVO studentScoreTotalDetailVO) {
        GradeTeacherR teacher = gradeTeacherRService.getTeacherBySubjectTypeAndGrade(studentScoreTotalDetailVO.getDeptUid(), studentScoreTotalDetailVO.getSubjectType());
        if(teacher != null){
            studentScoreTotalDetailVO.setTeacherUid(teacher.getUid());
            StudentScoreTotalDetail entity = studentScoreTotalDetailMapper.getTotalDetail(studentScoreTotalDetailVO);
            if(entity != null){
                entity.setTeacherUid(studentScoreTotalDetailVO.getTeacherUid());
                entity.setTotalScore(studentScoreTotalDetailVO.getTotalScore());
                entity.setTotalFull(studentScoreTotalDetailVO.getTotalFull());
                entity.updateById();
            }else {
                add(studentScoreTotalDetailVO);
            }
        }
        return ResultUtil.successWithMessage("更新成功");
    }

    @Override
    public String generateRank(StudentScoreTotalDetailVO studentScoreTotalDetailVO) {
        List<StudentScoreTotalDetail> list = studentScoreTotalDetailMapper.generateRank(studentScoreTotalDetailVO);
        for (StudentScoreTotalDetail detail: list) {
            detail.updateById();
        }
        return ResultUtil.successWithMessage("重新生成排名成功");
    }

    @Override
    public StudentScoreTotalDetailVO singleImport(StudentScoreTotalDetailVO studentScoreTotalDetailVO, StudentScoreBath studentScoreBath) {
        String companyCode = null;
        Dept suerDept = deptService.getDeptByName(studentScoreTotalDetailVO.getSuperDeptName());
        if(suerDept == null){
            throw new RuntimeException("上级机构不存在");
        }
        companyCode = suerDept.getCompanyCode();
        if(StringUtils.isNotBlank(studentScoreBath.getDeptUid())){
            Dept dept = deptService.getById(studentScoreBath.getDeptUid());
            if(dept != null){
                if(!companyCode.equals(dept.getCompanyCode())){
                    throw new RuntimeException("该机构不符合");
                }
            }else {
                Company company = companyService.getById(studentScoreBath.getDeptUid());
                if(company == null){
                    throw new RuntimeException("该机构不存在");
                }
                if(!companyCode.equals(company.getCompanyCode())){
                    throw new RuntimeException("该机构不符合");
                }
            }
        }
        String name = "";
        if(StringUtils.isNotBlank(studentScoreTotalDetailVO.getStudentName())){
            name = studentScoreTotalDetailVO.getStudentName();
        }
        String suffix = null;
        if(name.contains("(")){
            suffix = name.split("\\(")[1].substring(0,name.split("\\(")[1].length()-1);
            name = name.split("\\(")[0];
        }
        EmployeeVO employeeVO = new EmployeeVO();
        if(StringUtils.isNotBlank(name)){
            employeeVO.setName(name);
        }
        if(StringUtils.isNotBlank(name)){
            employeeVO.setNameEndWith(suffix);
        }
        if(StringUtils.isNotBlank(studentScoreTotalDetailVO.getStudentCode())){
            employeeVO.setEmployeeCode(studentScoreTotalDetailVO.getStudentCode());
        }
        employeeVO = employeeService.getEmployeeMesByCodeAndNameOrCertCodeAndSuffix(employeeVO);
        if(employeeVO == null){
            throw new RuntimeException("该学生不存在,或者已毕业");
        }

        if(StringUtils.isNotBlank(name) && !name.equals(employeeVO.getName())){
            throw new RuntimeException("该学生不存在");
        }
        studentScoreTotalDetailVO.setStudentUid(employeeVO.getUid());
        Grade grade;
        if(StringUtils.isNotBlank(studentScoreTotalDetailVO.getDeptName())){
            grade = gradeService.getGradeByName(companyCode,studentScoreTotalDetailVO.getDeptName());
        }else {
            grade = gradeService.getGradeByDeptCode(employeeVO.getCompanyCode());
        }
        if(grade == null){
            throw new RuntimeException("该班级不存在");
        }
        studentScoreTotalDetailVO.setDeptUid(grade.getUid());
        GradeTeacherR teacher = gradeTeacherRService.getTeacherBySubjectTypeAndGrade(grade.getUid(), studentScoreTotalDetailVO.getSubjectType());
        if(teacher == null){
            throw new RuntimeException("该班级不存在班主任");
        }
        studentScoreTotalDetailVO.setTeacherUid(teacher.getUid());
        studentScoreTotalDetailService.addScore(studentScoreTotalDetailVO);
        return studentScoreTotalDetailVO;
    }

    @Override
    public String generateRankTotalScore(StudentScoreTotalDetailVO studentScoreTotalDetailVO) {
        List<StudentScoreTotalDetailVO> list = studentScoreTotalDetailMapper.generateRankTotalScore(studentScoreTotalDetailVO);
        for (StudentScoreTotalDetailVO scoreTotalDetailVO : list) {
            editScore(scoreTotalDetailVO);
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }
}
